/************************************************************************************
 * MONASH UNIVERSITY, Faculty of Information Technology, Clayton School of IT.      *   
 * Student Declaration for FIT1008 Submission. I Menghong Li, ID:22988319           *
 * declare that this submission is my own work and has not been copied from any     *
 * other source without attribution. I acknowledge that severe penalties exist      *
 * for any copying of code without attribution, including a fail mark for this prac *
 ************************************************************************************/

package prac1;
public class App{
/**
 * Implements an application with name, author and price
 * 
 * @author             mli90
 * @since              28 July 2011
 * @input              name, author, price
 * @output             none
 * @knownBugs          none
 * @Constructor        App(String name, String author, double price)
 * 
 * @publicMethods      String getName()
 *                     void setName(String name)
 *                     String getAuthor()
 *                     void setAuthor(String author)
 *                     double getPrice()
 *                     void setPrice(double price)
 *                     int getNumberOfVotedPeople()
 *                     long getUniqueIdentifier()
 *                     double getTotalRating()
 *                     void vote(double voting)
 *                     double getRating()
 *                     String toString()
 */
	private String name;
	private String author;
	private double price;
	private int numberOfVotedPeople=0;
	private double totalRating=0;
	private static int nextId=0;
	private final int uniqueIdentifier = nextId++;
	
	/**
	 * Constructor
	 * @pre          name, author and price exist and are not empty
	 * @post         an App object has been created
	 * @param name   the name of the application
	 * @param author the author of the application
	 * @param price  the price of the application
	 */
	public App(String name, String author, double price) {
		
		//validate the inputs
		if(name == null || name==""){
			System.out.println("Name cannot be empty");
		}else{
			this.name = name;
		}
		if(author == null || author==""){
			System.out.println("Author cannot be empty");
		}else{
			this.author = author;
		}
		if(price <0){
			System.out.println("Price cannot lower than 0");
		}else{
			this.price = price;
		}
	}
	
	/**
	 * @return the name
	 * @pre    none
	 * @post   the name of the app is returned
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @param name the name to set
	 * @pre   the name to set exists and is not empty
	 * @post  the name is updated
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return the author
	 * @pre    none
	 * @post   the author of the app is returned
	 */
	public String getAuthor() {
		return author;
	}
	
	/**
	 * @param author the author to set
	 * @pre   the author to set exists and is not empty
	 * @post  the author is updated
	 */
	public void setAuthor(String author) {
		this.author = author;
	}
	
	/**
	 * @return the price
	 * @pre    none
	 * @post   the price of the app is returned
	 */
	public double getPrice() {
		return price;
	}
	
	/**
	 * @param price the price to set
	 * @pre   the price to set exists and must not lower than 0
	 * @post  the price is updated
	 */
	public void setPrice(double price) {
		this.price = price;
	}
	
	/**
	 * @return the numberOfVotedPeople
	 * @pre    none
	 * @post   the number of voted people is returned
	 */
	public int getNumberOfVotedPeople() {
		return numberOfVotedPeople;
	}
	
	/**
	 * @return the unique identifier
	 * 
	 * @pre      none
	 * @post     unique identifier is returned
	 * 
	 */
	public long getUniqueIdentifier() {
		return uniqueIdentifier;
	}

	/**
	 * @return the totalRating
	 * @pre    none
	 * @post   the total rating of the app is returned
	 */
	public double getTotalRating() {
		return totalRating;
	}
	/**
	 * @return none
	 * @param  voting the voting of the application
	 * @pre    the voting exist and 0<=voting<=5 (in half increments)
	 * @post   the total rating is updated and the number of voted people increased 1
	 */
	public void vote(double voting){
		if(voting<0||voting>5||voting%0.5!=0){
			System.out.println("Please Enter a vote from 0 to 5 (in half increments) e.g., 0, 0.5, 1, 1.5, etc.");
		}else{
			this.totalRating = getTotalRating() + voting;
			this.numberOfVotedPeople++;
		}
	}
	/**
	 * @pre    numberOfVotedPeople >0
	 * @post   the rating of the application is calculated and returned
	 * @return the rating of the application
	 */
	public int getRating(){
		int rating=0;
		if (numberOfVotedPeople ==0){
			System.out.println("There is currently no voting of this applicaton!");
		}else{
			rating =(int)(totalRating / numberOfVotedPeople)+1;
			
		}
		return rating;
	}
	
	/**
	 * 
	 * @return a string obtained by the name, author, price, identifier, ratings and number of votes of the application,each in a different line
	 * @pre    name, author, price, identifier, ratings and number of votes exist
	 * @post   a string obtained all of the information returned
	 */
	public String toString(){
		return "Name:" + getName()+'\n'+"Author:"+getAuthor()+'\n'+"Price:"+getPrice()+'\n'+"identifier:"+getUniqueIdentifier()+'\n'+
		"Ratings:"+getRating()+'\n'+"Number of votes:"+ getNumberOfVotedPeople();
	}
	
	/**
	 * main the method in the App class, test accessor and mutator methods
	 * @param args
	 */
	public static void main(String args[]){
		
		App firstApp= new App("firstApp", "Leo", 30);
		firstApp.vote(3);
		firstApp.vote(2.5);
	//	firstApp.vote(3);
	//	firstApp.vote(4);
		
		System.out.println(firstApp.toString());
		System.out.println();
		// mutate the name and test the accessor methods again
		firstApp.setName("MyFirstApp");
		System.out.println("Name:" +firstApp.getName());
		
		// mutate the author and test the accessor methods again
		firstApp.setAuthor("Menghong Li");
		System.out.println("Author:" +firstApp.getAuthor());
		
		// mutate the price and test the accessor methods again
		firstApp.setPrice(40);
		System.out.println("Price:"+firstApp.getPrice());
		
		System.out.println();
		//test the toString method again
		System.out.println(firstApp.toString());
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
	
}